/**
 * Copyright 2013 yangming.liu<liuyangming@gmail.com>.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, see <http://www.gnu.org/licenses/>.
 */
package org.bytesoft.openjta.coordinate.internal;

import javax.transaction.HeuristicCommitException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;

import org.bytesoft.openjta.TransactionStatus;
import org.bytesoft.openjta.archive.TransactionArchive;
import org.bytesoft.openjta.archive.XAResourceArchive;
import org.bytesoft.openjta.coordinate.TransactionCoordinateContext;
import org.bytesoft.openjta.coordinate.TransactionCoordinator;
import org.bytesoft.openjta.internal.RollbackRequiredException;
import org.bytesoft.openjta.internal.TransactionVote;
import org.bytesoft.openjta.resource.LocalTerminator;
import org.bytesoft.openjta.supports.TransactionLogger;

public class LROTransactionCoordinator extends RegularTransactionCoordinator implements TransactionCoordinator {

	public void processCommit(TransactionCoordinateContext context) throws HeuristicMixedException,
			HeuristicRollbackException, RollbackException, SecurityException, SystemException {

		TransactionStatus transactionStatus = context.getTransactionStatus();
		TransactionArchive transactionArchive = context.getTransactionArchive();
		TransactionLogger transactionLogger = context.getTransactionLogger();

		LocalTerminator localTerminator = context.getLocalTerminator();
		XAResourceArchive archive = localTerminator.getLastXAResourceArchive();
		archive.setPrepareStarted(true);
		archive.setPrepareResult(true);
		archive.setResourceVote(TransactionVote.XA_OK);

		boolean rollbackRequired = false;
		int vote = TransactionVote.XA_OK;
		try {
			vote = this.xaPrepare(context);
			if (vote == TransactionVote.XA_ROLLBACK_REQUIRED) {
				rollbackRequired = true;
			}
		} catch (RollbackRequiredException ex) {
			rollbackRequired = true;
		} catch (SystemException ex) {
			rollbackRequired = true;
		} catch (RuntimeException ex) {
			rollbackRequired = true;
		}

		if (rollbackRequired) {
			try {
				this.processRollback(context);
				throw new HeuristicRollbackException();
			} catch (HeuristicCommitException ex) {
				return;
			}
		}

		transactionStatus.transferToPrepared();
		transactionLogger.completePreparePhase(transactionArchive);

		try {
			localTerminator.lastResourceCommit();
		} catch (HeuristicMixedException ex) {
			rollbackRequired = true;
		} catch (HeuristicRollbackException ex) {
			rollbackRequired = true;
		} catch (SystemException ex) {
			rollbackRequired = true;
		} catch (RuntimeException ex) {
			rollbackRequired = true;
		}

		if (rollbackRequired) {
			transactionStatus.transferToRollingback();
			transactionLogger.enterRollbackPhase(transactionArchive);

			try {
				this.xaRollback(context);
				throw new HeuristicRollbackException();
			} catch (HeuristicCommitException ex) {
				// ignore
			}
		} else {
			transactionStatus.transferToCommitting();
			transactionLogger.enterCommitPhase(transactionArchive);

			boolean success = false;
			try {
				this.xaCommit(context);
				success = true;
			} finally {
				if (success) {
					transactionStatus.transferToCommitted();
				} else {
					transactionStatus.transferToCommitFail();
				}
				transactionLogger.completeCommitPhase(transactionArchive);
			}
		}

	}

	public void processRollback(TransactionCoordinateContext context) throws HeuristicMixedException,
			HeuristicCommitException, SystemException {

		this.xaRollback(context);
	}

	public void recoveryXACommit(TransactionCoordinateContext context) throws HeuristicMixedException,
			HeuristicRollbackException, RollbackException, SecurityException, SystemException {
		this.xaCommit(context);
	}

	public void recoveryXARollback(TransactionCoordinateContext context) throws HeuristicMixedException,
			HeuristicCommitException, SystemException {
		this.xaRollback(context);
	}

	public void processRecoveryCommit(TransactionCoordinateContext context) throws HeuristicMixedException,
			HeuristicRollbackException, RollbackException, SecurityException, SystemException {
		// TODO
		this.processCommit(context);
	}

	public void processRecoveryRollback(TransactionCoordinateContext context) throws HeuristicMixedException,
			HeuristicCommitException, SystemException {
		// TODO
		this.processRollback(context);
	}
}
